package com.three;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.fast.dev.frame.http.RequestParams;
import com.fast.dev.frame.http.callback.BaseHttpCallBack;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.sina.weibo.sdk.exception.WeiboException;
import com.wxtt.android.bean.UserBean;
import com.wxtt.android.config.ApiConfig;
import com.wxtt.android.config.ConstantConfig;

/**
 * 说明：新浪微博工具类
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/3/4 13:55
 * <p/>
 * 版本：verson 1.0
 */
public class WeiboUtils {

    //第三方应用可以使用自己的回调页
    public static final String APP_KEY = ConstantConfig.WEIBO_APPID;
    public static final String REDIRECT_URL = "https://api.weibo.com/oauth2/default.html";
    public static final String SCOPE = "follow_app_official_microblog";

    private SsoHandler mSsoHandler;
    private AuthInfo mAuthInfo;
    private static WeiboUtils mWeiboUtils;

    private WeiboUtils(Activity activity){
        mAuthInfo = new AuthInfo(activity,APP_KEY,REDIRECT_URL,SCOPE);
        mSsoHandler = new SsoHandler(activity,mAuthInfo);
    }

    public static synchronized WeiboUtils get(Activity activity){
        if (activity == null){
            throw new RuntimeException("新浪微博工具类---activity is null");
        }
        if (mWeiboUtils == null){
            mWeiboUtils = new WeiboUtils(activity);
        }
        return mWeiboUtils;
    }

    /**
     * 说明：当登录activity销毁时调用
     */
    public void destroy(){
        mSsoHandler = null;
        mAuthInfo = null;
        mWeiboUtils = null;
    }

    /**
     * 说明：新浪微博登录
     * @param listener
     */
    public void login(final WeiboLoginListener listener){
        mSsoHandler.authorize(new WeiboAuthListener(){
            @Override
            public void onComplete(Bundle bundle) {
                Oauth2AccessToken token = Oauth2AccessToken.parseAccessToken(bundle);
                if (token.isSessionValid()){
                    AccessTokenKeeper.writeAccessToken(token);
                    if (listener != null){
                        listener.success();
                    }
                }else {
                    if (listener != null){
                        listener.fail(bundle.getString("code"));
                    }
                }
            }

            @Override
            public void onWeiboException(WeiboException e) {
                if (listener != null){
                    listener.fail(e.getMessage());
                }
            }

            @Override
            public void onCancel() {
                if (listener != null){
                    listener.cancel();
                }
            }
        });
    }

    /**
     * 说明：当 SSO 授权 Activity 退出时，该函数被调用。
     * @param requestCode
     * @param resultCode
     * @param data
     */
    public void setAuthorizeCallBack(int requestCode, int resultCode, Intent data){
        if (mSsoHandler != null && data != null){
            mSsoHandler.authorizeCallBack(requestCode,resultCode,data);
        }
    }

    /**
     * 说明：判断是否登录超时
     * @return
     */
    public static boolean isLoginTimeOut(){
        return !AccessTokenKeeper.readAccessToken().isSessionValid();
    }

    /**
     * 说明：获取Token
     * @return
     */
    public static Oauth2AccessToken getToken(){
        return AccessTokenKeeper.readAccessToken();
    }

    /**
     * 说明：获取微博用户信息
     * @param callBack
     */
    public static void getUserInfo(BaseHttpCallBack callBack){
        ApiConfig.getWeiboUserInfo(getToken(),callBack);
    }

    /**
     * 说明：微博登录
     */
    public interface WeiboLoginListener{
        //登录成功
        void success();
        //登录失败
        void fail(String msg);
        //取消登录
        void cancel();
    }

}
